package liming.ai.data.deepseek;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 流式响应实体类
 */
public class ChatCompletionResponse {
    private String id;// 响应ID
    private String object;// 响应类型
    private Long created;// 响应创建时间
    private String model;// 模型
    private final JSONArray choices = new JSONArray();// 响应选项

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getObject() {
        return object;
    }

    public void setObject(String object) {
        this.object = object;
    }

    public Long getCreated() {
        return created;
    }

    public void setCreated(Long created) {
        this.created = created;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public JSONArray getChoices() {
        return choices;
    }

    public Choice getChoice(int index) {
        return Choice.valueOf(choices.optJSONObject(index));
    }


    public void addChoices(Choice choice) {
        this.choices.put(choice.toJson());
    }

    @Override
    public String toString() {
        return "ChatCompletionChunk{" +
                "id='" + id + '\'' +
                ", object='" + object + '\'' +
                ", created=" + created +
                ", model='" + model + '\'' +
                ", choices=" + choices +
                '}';
    }

    public String toJsonString() {
        return toJson().toString();
    }

    public JSONObject toJson() {
        JSONObject json = new JSONObject();
        json.put("id", id);
        json.put("object", object);
        json.put("created", created);
        json.put("model", model);
        json.put("choices", choices);
        return json;
    }

    public static ChatCompletionResponse valueOf(JSONObject jsonObject) {
        ChatCompletionResponse chatCompletionResponse = new ChatCompletionResponse();
        if (jsonObject.has("id"))
            chatCompletionResponse.setId(jsonObject.optString("id"));
        if (jsonObject.has("object"))
            chatCompletionResponse.setObject(jsonObject.optString("object"));
        if (jsonObject.has("created"))
            chatCompletionResponse.setCreated(jsonObject.optLong("created"));
        if (jsonObject.has("model"))
            chatCompletionResponse.setModel(jsonObject.optString("model"));
        if (jsonObject.has("choices")) {
            JSONArray choices = jsonObject.optJSONArray("choices");
            for (int i = 0; i < choices.length(); i++)
                chatCompletionResponse.addChoices(Choice.valueOf(choices.optJSONObject(i)));
        }
        return chatCompletionResponse;
    }


    /**
     * 响应选项实体类
     */
    public static class Choice {
        private Integer index;
        private Delta delta;
        private Object logprobs;
        private String finish_reason;

        public Integer getIndex() {
            return index;
        }

        public void setIndex(Integer index) {
            this.index = index;
        }

        public Delta getDelta() {
            return delta;
        }

        public void setDelta(Delta delta) {
            this.delta = delta;
        }

        public Object getLogprobs() {
            return logprobs;
        }

        public void setLogprobs(Object logprobs) {
            this.logprobs = logprobs;
        }

        public String getFinish_reason() {
            return finish_reason;
        }

        public void setFinish_reason(String finish_reason) {
            this.finish_reason = finish_reason;
        }

        @Override
        public String toString() {
            return "Choice{" +
                    "index=" + index +
                    ", delta=" + delta +
                    ", logprobs=" + logprobs +
                    ", finish_reason='" + finish_reason + '\'' +
                    '}';
        }

        public String toJsonString() {
            return toJson().toString();
        }

        public JSONObject toJson() {
            JSONObject json = new JSONObject();
            json.put("index", index);
            json.put("delta", delta.toJson());
            json.put("logprobs", logprobs);
            json.put("finish_reason", finish_reason);
            return json;
        }

        public static Choice valueOf(JSONObject jsonObject) {
            Choice choice = new Choice();
            if (jsonObject.has("index"))
                choice.setIndex(jsonObject.optInt("index"));
            if (jsonObject.has("delta")) {
                choice.setDelta(Delta.valueOf(jsonObject.optJSONObject("delta")));
            }
            if (jsonObject.has("logprobs"))
                choice.setLogprobs(jsonObject.opt("logprobs"));
            if (jsonObject.has("finish_reason"))
                choice.setFinish_reason(jsonObject.optString("finish_reason"));
            return choice;
        }
    }

    /**
     * 响应选项实体类
     */
    public static class Delta {
        private String role;
        private String content;

        private String reasoningContent;

        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getReasoningContent() {
            return reasoningContent;
        }

        public void setReasoningContent(String reasoningContent) {
            this.reasoningContent = reasoningContent;
        }

        @Override
        public String toString() {
            return "Delta{" +
                    "role='" + role + '\'' +
                    ", content='" + content + '\'' +
                    ", reasoningContent='" + reasoningContent + '\'' +
                    '}';
        }

        public String toJsonString() {
            return toJson().toString();
        }

        public JSONObject toJson() {
            JSONObject json = new JSONObject();
            json.put("role", role);
            json.put("content", content);
            json.put("reasoning_content", reasoningContent);
            return json;
        }

        public static Delta valueOf(JSONObject jsonObject) {
            Delta delta = new Delta();
            if (jsonObject.has("role"))
                delta.setRole(jsonObject.optString("role","system"));
            if (jsonObject.has("content"))
                delta.setContent(jsonObject.optString("content"));
            if (jsonObject.has("reasoning_content"))
                delta.setReasoningContent(jsonObject.optString("reasoning_content"));
            return delta;
        }
    }
}